#include "stdafx.h"
#include "Parsers.h"

/*
* This function parse the wig format file with .sea ext name from Tophat
* to get the low coverage regions. The result is stored in the vector ptr ocean.
*/
int getLowCoverageRegionFromWig(const char* wigFile, vector< pair<int,int> >& ocean, int coverageThreshold)
{
    const int geneNameCol = 0;
    const int rangeStartCol = 1;
    const int rangeEndCol = 2;
    const int coverageCol = 3;
    const int wigColNo = 4;

    if (!fileExist(wigFile)) {
        cout << "Can't not find File " << wigFile << endl;
        return(-1);
    }
    if (!hasTheExtName(wigFile, ".wig") && !hasTheExtName(wigFile, ".sea")) {
        cout << "File " << wigFile << " doesn't has a .wig ext name." << endl;
        return(-1);
    }
    ifstream ifile(wigFile);
    char oneline[MAX_LINE];
    for (int i = 0; ifile; i++) {
        ifile.getline(oneline, MAX_LINE);
        vector<string> wigLine;
        Tokenize(string(oneline), wigLine, "\t, ");
        if ((int)wigLine.size() == wigColNo) {
            int coverage = atoi(wigLine.at(coverageCol).c_str());
            if (coverage <= coverageThreshold) {
                pair<int, int> low_cov(
                    // TODO double check if the boundary of low coverage region is correct.
                    atoi(wigLine.at(rangeStartCol).c_str()),
                    atoi(wigLine.at(rangeEndCol).c_str()));
                if (low_cov.first <= low_cov.second) {
                    ocean.push_back(low_cov);
                }
            }
        } else {
            if ( i != 0 && isgraph((int)oneline[0])) {
                printf("\r Line %d:%s has differnt format", i, oneline);
            }
        }
    }
    return(ocean.size());
}

int getJunctionsFromBED(const char* filename, CJunctions& js, const char* pcaSeq)
{
    //const int geneNameCol = 0;
    const int rangeStartCol = 1;
    const int rangeEndCol = 2;
    //const int junctionName = 3;
    const int coverageCol = 4;
    const int strandCol = 5;

    if (!fileExist(filename)) {
        cout << "Can't not find File " << filename << endl;
        return(-1);
    }
    if (!hasTheExtName(filename, ".bed")) {
        cout << "File " << filename << " doesn't has a .bed ext name." << endl;
        return(-1);
    }
    ifstream ifile(filename);
    char oneline[MAX_LINE];
    for (int i = 0; ifile; i++) {
        ifile.getline(oneline, MAX_LINE);
        vector<string> bedLine;
        Tokenize(string(oneline), bedLine, "\t, ");
        if ((int)bedLine.size() > strandCol) {
            int coverage = atoi(bedLine.at(coverageCol).c_str());
            if (coverage > 0) {
                CJunction j;
                j.start = atoi(bedLine.at(rangeStartCol).c_str());
                j.end = atoi(bedLine.at(rangeEndCol).c_str());
                j.strand = bedLine.at(strandCol).at(0);
                j.support = coverage;
                // Use sequence to check the dirrection of the junction
                if (pcaSeq != NULL) {
                    int strandId = checkJunctionStrand(pcaSeq, j);
                    if (strandId == 0) {
                        j.strand = '+';
                    } else if (strandId == 1) {
                        j.strand = '-';
                    } else {
                        ; // non-standard junction
                    }
                }
                js.addJunction(j);
            }
        } else {
            if ( i != 0 && isgraph((int)oneline[0])) {
                printf("\rLine %d :%s has differnt format", i, oneline);
            }
        }
    }
    return(js.JunctionsV.size());
}